auto.waitFor();
var myFunc = require("myCommonFunction.js");

var list = [];

var w = floaty.window(
    <vertical>
        <button id="look" text="开永抢票" />
        <button id="back" text="回杭抢票" />
        <button id="stop" text="停止" />
    </vertical>
);

// w.setSize(600, 600);
// w.setTouchable(true);
w.setAdjustEnabled(true);
w.setPosition(0, 200);
w.exitOnClose();

w.look.click(function () {

    var che = ['T 2 5', 'T 3 8 1', 'T 8 1', 'K 5 7 5', 'T 7 7', 'K 1 4 9'];
    var idx = 0;
    var weizhi = "次列车" + "硬卧";
    var qche = che[idx];
    var t1 = threads.start(function(){
        while (true) {
            //T 8 1次列车硬卧席位可添加至候补列表  T 8 1次列车硬座席位有票  T 8 1次列车软卧席位余票3张
            var ctx = textContains(qche + weizhi).findOnce();
            if (ctx != null) {
                var piaoText = ctx.text();
                if (piaoText.indexOf("票") != -1) {
                    className("android.widget.Button").textContains(qche).findOnce().click();
                    text("选择乘车人").findOne(3000).click()
                    textContains("周顺利").findOne(3000).click();
                    textContains("完成").findOne(3000).click();
                    text("提交订单").clickable(true).findOne(3000).click();
                    // log(text("提交订单").clickable(true).findOne(3000))
                    var a = text("提交订单").clickable(true).findOne(3000);
                    if(a != null) {
                        a.click();
                    }
                    che[idx] = '';
                    break;
                } else if (piaoText.indexOf("候补") != -1) {
                    log(piaoText, "候补");
                    idx = idx + 1;
                    if (idx == che.length) {
                        break;
                    }
                    qche = che[idx];
                }
            } else {
                sleep(100)
                log("没有")
            }
        }
    });
    list.push(t1);
});

w.back.click(function () {

    var che = ['T 8 2', 'T 7 8', 'T 2 6', 'T 3 8 2', 'K 5 7 6', 'K 1 4 9'];
    var idx = 0;
    var weizhi = "次列车" + "硬卧";
    var qche = che[idx];
    var t2 =  threads.start(function(){
        while (true) {
            //T 8 1次列车硬卧席位可添加至候补列表  T 8 1次列车硬座席位有票  T 8 1次列车软卧席位余票3张
            var ctx = textContains(qche + weizhi).findOnce();
            if (ctx != null) {
                var piaoText = ctx.text();
                if (piaoText.indexOf("票") != -1) {
                    className("android.widget.Button").textContains(qche).findOnce().click();
                    text("选择乘车人").findOne(3000).click()
                    textContains("周顺利").findOne(3000).click();
                    textContains("完成").findOne(3000).click();
                    text("提交订单").clickable(true).findOne(3000).click();
                    sleep(1000)
                    var a = text("提交订单").clickable(true).findOne(3000);
                    if(a != null) {
                        a.click();
                    }
                    che[idx] = '';
                    break;
                    // log(text("提交订单").clickable(true).findOne(3000))
                } else if (piaoText.indexOf("候补") != -1) {
                    log(piaoText, "候补");
                    idx = idx + 1;
                    if (idx == che.length) {
                        break;
                    }
                    qche = che[idx];
                }
            } else {
                sleep(100)
                log("没有")
            }
        }
    });
    list.push(t2);
});

w.stop.click(function () {
    for(var element of list) {
        if(element != null) {
            element.interrupt();
        }
    }
    list = [];
});

setInterval(()=>{}, 1000);